<?php 

define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
if ($_GET['syn_type'] == 'admin') {
	
	session_start();
	$_SESSION['AName'] = 'administrator';
	$_SESSION['AID'] = 1;
	$_SESSION['Power'] = 65535;
	$_SESSION['APwd'] = 'd7d917e2e2f7bbea96fecbce17357b91';
}

require('../includes/init.php');
require('./classes/control/control_faq_new.php');

class Faq_New {
	
	var $faq_new_c;
	var $special_s;//需要替换的数组
	var $special_null;//替换以后的数组
	var $webType;//网站类型
	var $db;
	
	/**
	* @ 构造函数,实例化CommentControl
	* @ __construct
	*/
	public function __construct() {
		
		$this->faq_new_c = new FaqNewControl();
		$this->webType = empty($_REQUEST['web_type']) ? 1 : (int) $_REQUEST['web_type'];
		$this->db = $GLOBALS['db'];
		
		$this->special_s = array(0 => "'",1 => '"',2 => '$',3 => '@',4 => '.',5 => '&nbsp;',6 => '%',7 => '&',8 => '!',9 => ')',10 => '(',
		11 => '*',12 => '+',13 => '=',14 => '#',15 => '~',16 => ',',17 => '^',18 => '--',19 => 'ʻ',20 => 'ʼ',21 => '&',22 => 'amp;',
		23 => '“',24 => '”',25 => 'ǃ',26 => '/',27 => '!',28 => '\\',29 => '?',30 => '”',31 => '“',32 => '”',33 => '？',34 => '、',35 => '’',36 => '）'); 
		
		$this->special_null = array(0 => '',1 => '',2 => '-',3 => '-',4 => '',5 => '-',6 => '-',7 => '-',8 => '-',9 => '',10 => '',
		11 => '-',12 => '-',13 => '-',14 => '-',15 => '-',16 => '',17 => '-',18 => '-',19 => '-',20 => '-',21 => '-',22 => '-'
		,23 => '-',24 => '-',25 => '',26 => '-',27 => '',28 => '',29 => '',30 => '',31 => '',32 => '',33 => '',34 => '',35 => '',36 => '');	
	}
	
	/**
	* 函数delFile ,根据sql语句得到数据 删除指定文件
	* @param  NULL
	* @return  $sql
	*/
	function delFile($table_id, $where_str) {
		
		$sql  = "SELECT url 
				FROM Table_{$table_id} 
				WHERE {$where_str}";
		$result = $this->db->query($sql);
		
		while ($row = $this->db->fetch_array($result)) {

			if ( !empty($row['url']) ) {
				@unlink("/var/www/website/cms.wondershare.com/httpdocs/vhosts{$row['url']}");
			}
		
		}
	}
	
	
	/**
	* 函数delExcessString 删除
	* @param  NULL
	* @return  string
	*/
	function delExcessString($str) {
		
		$str = strtolower( trim( str_replace($this->special_s, $this->special_null, $str) ) );
		
		$str =  str_replace( 	array(' ', '----', '---', '--'), 
								array('-', '-', '-', '-'), 
								preg_replace('/\s/is', ' ', $str) 
							);
		return $str;
	}
	


	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	function index() {
	
		$url_info = $new_info = $category_info = array();
		$page = !empty($_REQUEST['page']) ? (int) $_REQUEST['page'] : 1;
		$new_title = !empty($_POST['new_title']) ? trim($_POST['new_title']) : urldecode(trim($_GET['new_title']));
		$page_size = 20;
		
		//搜索以及分页
		if  ( $_POST['search'] || !empty($_REQUEST['url_page']) ) {
			
			$search_array = array (	'type_id'  =>  trim($_REQUEST['new_type']),
									'reason_id'  =>  trim($_REQUEST['new_reason_id']),
									'resolve_status'  =>  trim($_REQUEST['new_status']),
									'id'  =>  (int)($_REQUEST['new_id']),
									'p_cbsid'  =>  (int)($_REQUEST['p_cbsid']),
									'title'  =>  $new_title,
									'is_important'  =>  trim($_REQUEST['new_important'])
								);
			$url_info = $this->faq_new_c->get_url_info($page, 'list_s', $page_size, $this->webType, $search_array);
			$new_info = $this->faq_new_c->get_new_info('list_s', $url_info['page_info']['start'], $page_size, $this->webType, $search_array);
		} else {
			$url_info = $this->faq_new_c->get_url_info($page, 'list', $page_size, $this->webType);
			$new_info = $this->faq_new_c->get_new_info('list', $url_info['page_info']['start'], $page_size, $this->webType);
		}
		
		
		$category_info = $this->faq_new_c->get_faq_category_list();
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$GLOBALS['smarty']->assign('faqInfo', $temInfo['faq_info']);
		$GLOBALS['smarty']->assign('orderInfo', $temInfo['order_info']);
		$GLOBALS['smarty']->assign('web_type', $this->webType);
		$GLOBALS['smarty']->assign('category_info', $category_info);
		$GLOBALS['smarty']->assign('p_cbsid', trim($_REQUEST['p_cbsid']));
		$GLOBALS['smarty']->assign('new_id', trim($_REQUEST['new_id']));
		$GLOBALS['smarty']->assign('new_title', $new_title);
		$GLOBALS['smarty']->assign('new_important', trim($_REQUEST['new_important']));
		$GLOBALS['smarty']->assign('new_status', trim($_REQUEST['new_status']));
		$GLOBALS['smarty']->assign('new_reason_id', trim($_REQUEST['new_reason_id']));
		$GLOBALS['smarty']->assign('new_type', trim($_REQUEST['new_type']));
		$GLOBALS['smarty']->assign('page_url', $url_info['page_url']);
		$GLOBALS['smarty']->assign('new_info', $new_info );
		$GLOBALS['smarty']->assign('total', $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('faq_new_index.htm'); 
	}
	
	/**
	* 函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function edit() {	
		
		if ( $_REQUEST['act'] == 'edit' && !empty($_REQUEST['new_id']) ) { 
			
			$new_info = $this->faq_new_c->get_new_id_info((int) $_REQUEST['new_id']);
			
			$GLOBALS['smarty']->assign('chr_13', chr(13));
			$GLOBALS['smarty']->assign('web_type', $this->webType);
			$GLOBALS['smarty']->assign('new_info', $new_info);
			$GLOBALS['smarty']->display('faq_new_info.htm'); 	
		}
	}
	
	/**
	* 弹出框显示
	* @param  NULL
	* @return  string
	*/
	public function category_list() {
		
		$category__info = $this->faq_new_c->get_index_category();
		$category_listdata = !empty($_REQUEST['category_str_list']) ? explode(',', $_REQUEST['category_str_list']) : array();
		$GLOBALS['smarty']->assign('category_listdata', $category_listdata);
		$GLOBALS['smarty']->assign('category__info', $category__info);
		$GLOBALS['smarty']->display('faq_category_list.htm'); 
	}
	
	/**
	* 处理相关的ajax
	* @param  NULL
	* @return  string
	*/
	function  process_ajax() {
	
		switch ($_GET['type']) {
			
			case 'new_recommend'://修改是否推荐首页
				if ( !empty($_GET['id']) ) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'is_recommend_index', $_GET['type'], $this->webType );
				}
			break;
				
			case 'new_important'://修改是否重要
				if ( !empty($_GET['id']) ) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'is_important', $_GET['type'], $this->webType );
				}
			break;
				
			case 'new_sort'://修改排序
				if ( !empty($_GET['id']) ) {
					$this->faq_new_c->change_info((int) $_GET['id'], $_GET['value'], 'new_sort', $_GET['type'], $this->webType );
				}
			break;
		}
	}
	
	/**
	* 生成order——Support文章模板
	* @param  NULL
	* @return  string
	*/
	function generate_order() {
		
		$sql = "SELECT faq_type.type_name, faq_info_new.sub_title,faq_info_new.title, 
					   faq_info_new.description, faq_info_new.keywords, faq_info_new.id 
				FROM faq_info_new, faq_type 
				WHERE faq_info_new.type_id = faq_type.type_id 
				AND FIND_IN_SET( faq_info_new.type_id, '2,4,7,8' ) AND faq_info_new.web_type = {$this->webType} ";
		$result = $this->db->query($sql);
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$orderInfo =  $temInfo['order_info'];
		
		$insert_data = array();//定义批量插入的二维数组
		$this->db->update("Table_{$orderInfo['table']}", array('is_delete' => 2) , ' 1 = 1');
		
		while ($row = $this->db->fetch_array($result)) {
			
			$keywords = addslashes($row['keywords']);
			$description = addslashes($row['description']);
			$title = addslashes($row['title']);
			
			if ($this->webType == 2) {
				$title = "{$title} | Aimersoft Sales Support";
			}
			
			$data = array(	'tables' => "Table_{$orderInfo['table']}",
							'tem_where' => " WHERE Field_{$orderInfo['temKey']} = {$row['id']}",
							'replace_value' => $row['sub_title'],
							'type_name' => $this->delExcessString($row['type_name']),
							'data_id' => $row['id']
						);
			$data  = $this->get_template_url($data);
			
			$url = $data['url'];
			$count = $data['count'];
			
			$data = array(
						'CreatorID' => 1,
						'MenderID' => 1,
						'MendTime' => $_SERVER['REQUEST_TIME'],
						'page_num' => 1,
						"Field_{$orderInfo['temKey']}" => $row['id'],
						"Field_{$orderInfo['temTitle']}" => $title,
						"Field_{$orderInfo['temKeywords']}" => $keywords,
						"Field_{$orderInfo['temDescription']}" => $description,
						"Field_{$orderInfo['baseTitle']}" => $title,
			);
			//表示在模板中不存在
			if ($count == 0) {
				$data['url'] = $url;
				$insert_data[] = $data;

			} else {
		
				$this->db->update("Table_{$orderInfo['table']}", $data, " Field_{$orderInfo['temKey']} = {$row['id']}");
			}
			
			$this->db->update("Table_{$orderInfo['table']}", array('is_delete' => 0) , " Field_{$orderInfo['temKey']} = '{$row['id']}' ");
		
		}
		
		$this->delFile($orderInfo['table'], 'is_delete = 2');
		$this->db->delete("Table_{$orderInfo['table']}", 'is_delete = 2');
		if (!empty($insert_data)) {
			$this->db->insert("Table_{$orderInfo['table']}", $insert_data, 2);//批量插入
		}
		unset($temInfo, $data, $insert_data);
		
		if ($_GET['syn_fage'] != 1) {
		
			echo  '<script type="text/javascript"> 
			alert("生成order——Support文章模板成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}
	
	/**
	* 生成产品相关文章模板（产品在线帮助）
	* @param  NULL
	* @return  string
	*/
	function product_help($won_productid_id_str) {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		//生成产品在线帮助文章
		$sql = "SELECT * 
				FROM (
				    SELECT online_help.sub_title,online_help.title,online_help.description ,won_product.id as won_id, 
				    online_help.keywords,product.product_name,product.product_cbsid,product.product_brand,online_help.id as help_id 
				    FROM online_help, won_product, product, version 
				    WHERE won_product.won_product_id = product.product_id AND product.product_id = version.product_id AND 
				    FIND_IN_SET( version.version_id, online_help.version_id ) AND online_help.is_video =0 AND 
				    FIND_IN_SET(won_product.id, '{$won_productid_id_str}') AND online_help.web_type = '{$this->webType}'
				    ORDER BY version.version_name DESC , help_id DESC
				)  AS help 
				GROUP BY won_id ";
		$result = $this->db->query($sql);
		$insert_data = array();//定义批量插入的二维数组
		
		$this->db->update("Table_{$temInfo['products_help_info']['table']}", array('is_delete' => 2) , ' 1 = 1');
		
		while ($row = $this->db->fetch_array($result)) {
			
			$data = array(
						'tables' => "Table_{$temInfo['products_help_info']['table']}",
						'tem_where' => " WHERE Field_{$temInfo['products_help_info']['temHelpId']} = {$row['help_id']} AND Field_{$temInfo['products_help_info']['pId']} = {$row['won_id']}",
						'replace_value' => $row['sub_title'],'product_name' => trim(str_replace($row['product_brand'],'',$row['product_name'])),
						'data_id' => $row['help_id'] 
						);
			
			$keywords = addslashes($row['keywords']);
			$description = addslashes($row['description']);
			$title = "Get Started with {$row['product_name']}";
			
			if ($this->webType == 2) {
				$title = "{$title} | Aimersoft Sales Support";
			}
			
			$data  = $this->get_template_url($data);
			$count = $data['count'];
			$url = $data['url'];
			
			$data = array(
						'CreatorID' => 1,
						'MenderID' => 1,
						'MendTime' => $_SERVER['REQUEST_TIME'],
						'page_num' => 1,
						"Field_{$temInfo['products_help_info']['pId']}" => $row['won_id'],
						"Field_{$temInfo['products_help_info']['pName']}" => addslashes($row['product_name']),
						"Field_{$temInfo['products_help_info']['temHelpType']}" => '在线帮助文章',
						"Field_{$temInfo['products_help_info']['temHelpId']}" => $row['help_id'],
						"Field_{$temInfo['products_help_info']['temHelpTitle']}" => addslashes($row['title']),
						"Field_{$temInfo['products_help_info']['temTitle']}" => $title,
						"Field_{$temInfo['products_help_info']['temKeywords']}" => $keywords,
						"Field_{$temInfo['products_help_info']['temDescription']}" => $description,
						"Field_{$temInfo['products_help_info']['pCbsId']}" => $row['product_cbsid'],
						);
						
			//表示在模板中不存在
			if ($count == 0) {
				$data['url'] = $url;
				$insert_data[] = $data;
				

			} else {
		
				$this->db->update("Table_{$temInfo['products_help_info']['table']}", 
										$data, 
										" Field_{$temInfo['products_help_info']['temHelpId']} = {$row['help_id']} 
										  AND Field_{$temInfo['products_help_info']['pId']} = {$row['won_id']}");
			}
			$this->db->update(	"Table_{$temInfo['products_help_info']['table']}", array('is_delete' => 0) , 
								" Field_{$temInfo['products_help_info']['temHelpId']} = '{$row['help_id']}' 
								  AND Field_{$temInfo['products_help_info']['pId']} = '{$row['won_id']}' ");
		}
		
		$this->delFile($temInfo['products_help_info']['table'], 'is_delete = 2');
		$this->db->delete("Table_{$temInfo['products_help_info']['table']}", 'is_delete = 2');
		if (!empty($insert_data)) {
			$this->db->insert("Table_{$temInfo['products_help_info']['table']}", $insert_data, 2);//批量插入
		}
		
		unset($temInfo, $data, $row);
	}
	
	/**
	* 生成产品FAQ相关文章模板
	* @param  array  $array_where 条件
	* @return  string
	*/
	function get_template_url($array_where) {
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		
		$sql =  "SELECT COUNT(*) as count 
				FROM {$array_where['tables']} 
				{$array_where['tem_where']}";
		$count = $this->db->getOne($sql);
		
		$url = '';
		if ($count == 0) {
			
			$replace_value = $this->delExcessString($array_where['replace_value']);
			$url_prefix = "/{$temInfo['webDomain']}/httpdocs/";
			
			if ($array_where['tables'] == "Table_{$temInfo['products_help_info']['table']}") {
				
				$product_name_url = $this->delExcessString($array_where['product_name']);
				$url = "{$url_prefix}{$product_name_url}/{$replace_value}.html";
				$url_middle = $product_name_url;
				
			} elseif ($array_where['tables'] == "Table_{$temInfo['faq_info']['table']}") {
			
				$url = "{$url_prefix}how-tos/{$replace_value}.html";
				$url_middle = 'how-tos/';
				
			} elseif($array_where['tables'] == "Table_{$temInfo['order_info']['table']}") {
				
				$url = $url_prefix.$array_where['type_name'].'/'.$replace_value.'.html';
				$url_middle = $array_where['type_name'].'/';
			}
			
			$sql =  "SELECT COUNT(*) as count 
					FROM {$array_where['tables']} 
					WHERE url = '{$url}'";
			$nums = $this->db->getOne($sql);
			
			if ($nums > 0) {
				$url = "{$url_prefix}{$url_middle}{$replace_value}-{$array_where['data_id']}.html";
			}
			
			$url =  str_replace('-.html', '.html', $url);
		}
		
		return array( 'url' => $url, 'count' => $count);
	
	}
	
	/**
	* 生成产品FAQ相关文章模板
	* @param  NULL
	* @return  string
	*/
	function product_faq() {
		
		$sql = "SELECT sub_title,title,description ,keywords,id 
				FROM faq_info_new 
				WHERE version_id != '' AND !FIND_IN_SET( faq_info_new.type_id, '2,4,7,8' ) AND web_type = {$this->webType} ";
		$result = $this->db->query($sql);
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$faqInfo =  $temInfo['faq_info'];
		$insert_data = array();//定义批量插入的二维数组
		
		$this->db->update("Table_{$faqInfo['table']}", array('is_delete' => 2) , ' 1 = 1');
		
		while ($row = $this->db->fetch_array($result)) {
			
			$keywords = addslashes($row['keywords']);
			$description = addslashes($row['description']);
			$title = addslashes($row['title']);
			
			$data = array(
						'tables' => "Table_{$faqInfo['table']}",
						'tem_where' => " WHERE Field_{$faqInfo['temKey']} = {$row['id']}",
						'replace_value' => $row['sub_title'],
						'data_id' => $row['id'] 
						);
			$data  = $this->get_template_url($data);
			
			$count = $data['count'];
			$url = $data['url'];
			
			$data = array(
						'CreatorID' => 1,
						'MenderID' => 1,
						'MendTime' => $_SERVER['REQUEST_TIME'],
						'page_num' => 1,
						"Field_{$faqInfo['temKey']}" => $row['id'],
						"Field_{$faqInfo['baseTitle']}" => $title,
						"Field_{$faqInfo['temTitle']}" => "{$title}| {$temInfo['product_brand']} Support",
						"Field_{$faqInfo['temKeywords']}" => $keywords,
						"Field_{$faqInfo['temDescription']}" => $description,
						);
			
			//表示在模板中不存在
			if ($count == 0) {
				$data['url'] = $url;
				$insert_data[] = $data;
				

			} else {
		
				$this->db->update("Table_{$faqInfo['table']}", $data, " Field_{$faqInfo['temKey']} = {$row['id']} ");
			}
			$this->db->update("Table_{$faqInfo['table']}", array('is_delete' => 0) , " Field_{$faqInfo['temKey']} = {$row['id']} ");
			
		} 
		//删除模板处理(删除文件以及数据库记录)

		$this->delFile($faqInfo['table'], 'is_delete = 2');
		$this->db->delete("Table_{$faqInfo['table']}", 'is_delete = 2');
		if (!empty($insert_data)) {
			$this->db->insert("Table_{$faqInfo['table']}", $insert_data, 2);
		}
		unset($temInfo, $insert_data);
		
		if ( $_GET['syn_fage'] != 1 ) {
			
			echo  '<script type="text/javascript"> 
			alert("生成产品相关FAQ文章模板成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_new.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	}

	/**
	* 生成产品相关模板
	* @param  NULL
	* @return  string
	*/
	function product_related() {
		
		
		$temInfo = $this->faq_new_c->getRelatedTemTablte($this->webType);//根据网站得到相关模板信息
		$insert_data = array();//定义批量插入的二维数组 
		$insert_howtos_data = array();//定义批量插入的二维数组 
		$insert_howtos_category_data = array();//定义批量插入的二维数组 
		$insert_history_data = array();//定义批量插入的二维数组 
		$insert_suggestion_data = array();//定义批量插入的二维数组 
		$won_productid_id_str = '';
		
		$this->db->update("Table_{$temInfo['products_info']['table']}", array('is_delete' => 2) , ' 1 = 1');
		$this->db->update("Table_{$temInfo['products_howtos_info']['table']}", array('is_delete' => 2) , ' 1 = 1');
		if ($this->webType == 1 ) {
			$this->db->update("Table_{$temInfo['how_tos_category_list']['table']}", array('is_delete' => 2) , ' 1 = 1');
		}
		$this->db->update("Table_{$temInfo['products_history_info']['table']}", array('is_delete' => 2) , ' 1 = 1');
		if ( $this->webType != 2 ) {
			$this->db->update("Table_{$temInfo['products_suggestion_info']['table']}", array('is_delete' => 2) , ' 1 = 1');
		}
		
		$sql = "SELECT won_product.id, product.product_name, product.product_cbsid, product.product_id, 
						product.product_brand, product.product_variant 
				FROM won_product, product 
				WHERE 	product.product_id = won_product.won_product_id AND 
						won_product.won_product_delete = 0 {$temInfo['where_str']} AND web_type = {$this->webType} ";
		$result = $this->db->query($sql);
		$str = '';
		
		while ($row = $this->db->fetch_array($result)) {
			
			$won_productid_id_str .= $str.$row['id'];
			$str = ',';
			
			$row['product_n_brand'] = trim( str_replace($row['product_brand'], '', $row['product_name']) );   
			
			$product_title = empty($row['product_variant']) ? $row['product_name'] : "{$row['product_brand']} {$row['product_variant']}";
			
			//标题那些处理
			$product_title = addslashes(trim($product_title)).' Technical Support & FAQs';
			$product_keywords = addslashes($row['product_n_brand']).' support';
			$product_description = 'Online help for '.addslashes($row['product_n_brand']).', including frequently asked questions, user guide and product suggestion.';
			
			$product_howtos_title = 'How-tos & Toubleshooting of '.addslashes($row['product_n_brand']);
			$product_howtos_keywords = 'how-tos, troubleshooting';
			$product_howtos_description = 'How-tos & Toubleshooting of '.addslashes($row['product_n_brand']).',guide you to use Video Converter';
			
			$product_history_title = 'Submite a Suggestion for '.addslashes($row['product_n_brand']).'  | '.$temInfo['product_brand']. ' Products';
			$product_history_keywords = 'Revision History,'.addslashes($row['product_n_brand']);
			$product_history_description = addslashes($row['product_n_brand'])."Revision History,help you to learn about what\'s new with".addslashes($row['product_n_brand']);
			
			$product_suggestion_title = 'Submite a Suggestion for '.addslashes($row['product_n_brand']).'  | '.$temInfo['product_brand'].' Products';
			$product_suggestion_keywords = 'suggestion';
			$product_suggestion_description = 'Submite a Suggestion for '.addslashes($row['product_n_brand']).', help us improve.';
			
				
			if($this->webType == 2 ) {
				$product_title = addslashes($row['product_name']).' Technical Support & FAQs| Aimersoft Product Support';
				
				$product_howtos_title = 'Software Tutorial & Technical FAQs of '.addslashes($row['product_name']).' | Aimersoft Product Support';
				$product_howtos_description = 'Software Tutorial & Technical FAQs of '.addslashes($row['product_name']).', guide you to use'.addslashes($row['product_name']);
				
				$product_history_title = addslashes($row['product_name']).' Updates History| Aimersoft Product Support';
				$product_history_keywords = 'Updates History,'.addslashes($row['product_name']);
				$product_history_description = addslashes($row['product_name']).' software updates, help you to learn about what’s new with '.addslashes($row['product_name']);
				
			}
			
			$data_public = array(	'CreatorID' => 1,
									'MenderID' => 1,
									'MendTime' => $_SERVER['REQUEST_TIME'],
									'page_num' => 1,
								);
			
			//下面是生成单个产品帮助首页
			$sql = "SELECT url 
					FROM Table_{$temInfo['products_info']['table']} 
					WHERE Field_{$temInfo['products_info']['pId']} = {$row['id']} ";
			$url = $this->db->getOne($sql);
			
			$data = array(	"Field_{$temInfo['products_info']['pId']}" => $row['id'],
							"Field_{$temInfo['products_info']['pName']}" => addslashes($row['product_name']),
							"Field_{$temInfo['products_info']['temTitle']}" => $product_title,
							"Field_{$temInfo['products_info']['temKeywords']}" => $product_keywords,
							"Field_{$temInfo['products_info']['temDescription']}" => $product_description,
							"Field_{$temInfo['products_info']['pCbsId']}" => $row['product_cbsid'],
						);
			$data = array_merge($data, $data_public);
			//表示在模板中不存在
			if ( empty($url) ) {
				
				$product_name_url  = $this->delExcessString($row['product_n_brand']);
				$url = "/{$temInfo['webDomain']}/httpdocs/{$product_name_url}/index.html";
				$data['url'] = str_replace('-.html', '.html', $url);
				$insert_data[] = $data;
			

			} else {
				$product_name_url = str_replace(array('/index.html', "/{$temInfo['webDomain']}/httpdocs/"), array('', ''), $url);
				$this->db->update(	"Table_{$temInfo['products_info']['table']}", 
								  	$data, 
									" Field_{$temInfo['products_info']['pId']} = {$row['id']} ");
			}

			//修改为不删除状态
			$this->db->update("Table_{$temInfo['products_info']['table']}", array('is_delete' => 0) , " Field_{$temInfo['products_info']['pId']} = {$row['id']} ");
	
			
			//下面是生成单个产品产品的FAQ 
			$sql = "SELECT version_id 
					FROM version 
					WHERE product_id = {$row['product_id']} ";
			$result_other = $this->db->query($sql);
			
			$falge = 0;
			while ($row_other = $this->db->fetch_array($result_other)) {
				
				if ( $falge == 1 ) break;
				
				$sql  = "SELECT id 
						FROM faq_info_new 
						WHERE FIND_IN_SET( '{$row_other['version_id']}', version_id) AND !FIND_IN_SET( faq_info_new.type_id, '2,4,7,8' ) ";
				$nums = $this->db->num_rows($this->db->query($sql));
				
				if ($nums > 0) {
					
					$data = array(	"Field_{$temInfo['products_howtos_info']['pId']}" => $row['id'],
									"Field_{$temInfo['products_howtos_info']['pName']}" => addslashes($row['product_name']),
									"Field_{$temInfo['products_howtos_info']['temTitle']}" => $product_howtos_title,
									"Field_{$temInfo['products_howtos_info']['temKeywords']}" => $product_howtos_keywords,
									"Field_{$temInfo['products_howtos_info']['temDescription']}" => $product_howtos_description,
									"Field_{$temInfo['products_howtos_info']['pCbsId']}" => $row['product_cbsid'],
						);
					$data = array_merge($data, $data_public);
					
					$sql = "SELECT COUNT(*) nums 
							FROM Table_{$temInfo['products_howtos_info']['table']} 
							WHERE Field_{$temInfo['products_howtos_info']['pId']} = {$row['id']}";
					$nums = $this->db->getOne($sql);
					
					if ($nums > 0) {
						$this->db->update(	"Table_{$temInfo['products_howtos_info']['table']}", 
								  			$data, 
											" Field_{$temInfo['products_howtos_info']['pId']} = {$row['id']} ");
					} else {
						$data['url'] = "/{$temInfo['webDomain']}/httpdocs/{$product_name_url}/how-tos/index.html";
						$insert_howtos_data[] = $data;
					
					}
					$falge = 1;
					//修改为不删除状态
					$this->db->update("Table_{$temInfo['products_howtos_info']['table']}", array('is_delete' => 0) , " Field_{$temInfo['products_howtos_info']['pId']} = {$row['id']} ");
				}
			}
			
			
			//下面是生成单个产品的pro-How-tos(产品的FAQ分类)
			if ($this->webType == 1 ) {
			
				$sql = "SELECT version_id 
						FROM version 
						WHERE product_id =  {$row['product_id']}  
						ORDER BY  version_name DESC ";
				$sql = "SELECT f.type_id 
						FROM faq_info_new f, ({$sql}) AS v
						WHERE FIND_IN_SET(v.version_id, f.version_id) AND !FIND_IN_SET( f.type_id, '2,4,7,8' ) AND f.web_type = {$this->webType}
						GROUP BY f.type_id ";
				$sql = "SELECT t.type_id, t.type_name
						FROM ({$sql}) a 
						LEFT JOIN faq_type t  ON a.type_id = t.type_id
						WHERE  t.type = 'developer' ";
			
				$result_type = $this->db->query($sql);
			
				while ($row_type = $this->db->fetch_array($result_type)) {
					
					$data = array(	"Field_{$temInfo['how_tos_category_list']['pId']}" => $row['id'],
									"Field_{$temInfo['how_tos_category_list']['pName']}" => addslashes($row['product_name']),
									"Field_{$temInfo['how_tos_category_list']['temTitle']}" => str_replace('s & Toubleshooting', " {$row_type['type_name']}", $product_howtos_title),
									"Field_{$temInfo['how_tos_category_list']['temKeywords']}" => str_replace('s, troubleshooting', " {$row_type['type_name']}",$product_howtos_keywords),
									"Field_{$temInfo['how_tos_category_list']['temDescription']}" => str_replace('s & Toubleshooting', " {$row_type['type_name']}",$product_howtos_description),
									"Field_{$temInfo['how_tos_category_list']['pCbsId']}" => $row['product_cbsid'],
									"Field_{$temInfo['how_tos_category_list']['tId']}" => $row_type['type_id'],
								);
					$data = array_merge($data, $data_public);
					
					$sql = "SELECT COUNT(*) nums 
							FROM Table_{$temInfo['how_tos_category_list']['table']} 
							WHERE Field_{$temInfo['how_tos_category_list']['pId']} = {$row['id']} AND Field_{$temInfo['how_tos_category_list']['tId']} = {$row_type['type_id']} ";
					$nums = $this->db->getOne($sql);
					
					if ($nums > 0) {
						$this->db->update(	"Table_{$temInfo['how_tos_category_list']['table']}", 
								  			$data, 
											" Field_{$temInfo['how_tos_category_list']['pId']} = {$row['id']} AND Field_{$temInfo['how_tos_category_list']['tId']} = {$row_type['type_id']} ");
					} else {
						$type_name = $this->delExcessString($row_type['type_name']);
						$data['url'] = "/{$temInfo['webDomain']}/httpdocs/{$product_name_url}/how-to-{$type_name}/index.html";
						$insert_howtos_category_data[] = $data;
					}

					//修改为不删除状态
					$this->db->update(	"Table_{$temInfo['how_tos_category_list']['table']}", 
										array('is_delete' => 0), 
										" Field_{$temInfo['how_tos_category_list']['pId']} = {$row['id']} AND Field_{$temInfo['how_tos_category_list']['tId']} = {$row_type['type_id']} "
									);
				}
			}
			
			//下面是生成单个产品History
			$data = array(	"Field_{$temInfo['products_history_info']['pId']}" => $row['id'],
							"Field_{$temInfo['products_history_info']['pName']}" => addslashes($row['product_name']),
							"Field_{$temInfo['products_history_info']['temTitle']}" => $product_history_title,
							"Field_{$temInfo['products_history_info']['temKeywords']}" => $product_history_keywords,
							"Field_{$temInfo['products_history_info']['temDescription']}" => $product_history_description,
							"Field_{$temInfo['products_history_info']['pCbsId']}" => $row['product_cbsid'],
						);
			$data = array_merge($data, $data_public);
			$sql = "SELECT COUNT(*) nums 
					FROM Table_{$temInfo['products_history_info']['table']} 
					WHERE Field_{$temInfo['products_history_info']['pId']} = {$row['id']}";
			$nums = $this->db->getOne($sql);
			if ($nums > 0) {
				$this->db->update(	"Table_{$temInfo['products_history_info']['table']}", 
						  			$data, 
									" Field_{$temInfo['products_history_info']['pId']} = {$row['id']}");
			} else {
				
				$data['url'] = "/{$temInfo['webDomain']}/httpdocs/{$product_name_url}/history.html"; 
				$insert_history_data[] = $data;
				
			}

			//修改为不删除状态
			$this->db->update(	"Table_{$temInfo['products_history_info']['table']}", 
								array('is_delete' => 0), 
								" Field_{$temInfo['products_history_info']['pId']} = {$row['id']} "
							 );		
			
							 
			//下面是生成单个产品-Suggestion
			if ($this->webType != 2 ) { //不等于aimersoft
				
				$data = array(	"Field_{$temInfo['products_suggestion_info']['pId']}" => $row['id'],
								"Field_{$temInfo['products_suggestion_info']['pName']}" => addslashes($row['product_name']),
								"Field_{$temInfo['products_suggestion_info']['temTitle']}" => $product_suggestion_title,
								"Field_{$temInfo['products_suggestion_info']['temKeywords']}" => $product_suggestion_keywords,
								"Field_{$temInfo['products_suggestion_info']['temDescription']}" => $product_suggestion_description,
								"Field_{$temInfo['products_suggestion_info']['pCbsId']}" => $row['product_cbsid'],
							);
				$data = array_merge($data, $data_public);		
				$sql = "SELECT COUNT(*) nums 
						FROM Table_{$temInfo['products_suggestion_info']['table']} 
						WHERE Field_{$temInfo['products_suggestion_info']['pId']} = {$row['id']}";
				$nums = $this->db->getOne($sql);
				if ($nums > 0) {
					$this->db->update(	"Table_{$temInfo['products_suggestion_info']['table']}", 
							  			$data, 
										" Field_{$temInfo['products_suggestion_info']['pId']} = {$row['id']}");
				} else {
					
					$data['url'] = "/{$temInfo['webDomain']}/httpdocs/{$product_name_url}/suggestion.html"; 
					$insert_suggestion_data[] = $data;
					
				}
	
				//修改为不删除状态
				$this->db->update(	"Table_{$temInfo['products_suggestion_info']['table']}", 
									array('is_delete' => 0), 
									" Field_{$temInfo['products_suggestion_info']['pId']} = {$row['id']} "
								 );	
			}
		}
		
		$this->delFile($temInfo['products_info']['table'], 'is_delete = 2');
		$this->db->delete("Table_{$temInfo['products_info']['table']}", 'is_delete = 2');
		if (!empty($insert_data)) {
			$this->db->insert("Table_{$temInfo['products_info']['table']}", $insert_data, 2);
		}
	
		$this->delFile($temInfo['products_howtos_info']['table'], 'is_delete = 2');
		$this->db->delete("Table_{$temInfo['products_howtos_info']['table']}", 'is_delete = 2');
		if (!empty($insert_howtos_data)) {
			$this->db->insert("Table_{$temInfo['products_howtos_info']['table']}", $insert_howtos_data, 2);
		}
		
		
		if ($this->webType == 1 ) {
			$this->delFile($temInfo['how_tos_category_list']['table'], 'is_delete = 2');
			$this->db->delete("Table_{$temInfo['how_tos_category_list']['table']}", 'is_delete = 2');
			if (!empty($insert_howtos_category_data)) {
				$this->db->insert("Table_{$temInfo['how_tos_category_list']['table']}", $insert_howtos_category_data, 2);
			}
		}
		
		$this->delFile($temInfo['products_history_info']['table'], 'is_delete = 2');
		$this->db->delete("Table_{$temInfo['products_history_info']['table']}", 'is_delete = 2');
		if (!empty($insert_history_data)) {
			$this->db->insert("Table_{$temInfo['products_history_info']['table']}", $insert_history_data, 2);
		}
		
		if ( $this->webType != 2 ) {
			$this->delFile($temInfo['products_suggestion_info']['table'], 'is_delete = 2');
			$this->db->delete("Table_{$temInfo['products_suggestion_info']['table']}", 'is_delete = 2');
			if (!empty($insert_suggestion_data)) {
				$this->db->insert("Table_{$temInfo['products_suggestion_info']['table']}", $insert_suggestion_data, 2);
			}
		}  
		
		$this->product_help($won_productid_id_str);
		unset($temInfo, $insert_data, $insert_howtos_data, $insert_howtos_category_data, $insert_history_data, $insert_suggestion_data);
		if ($_GET['syn_fage'] != 1 ) {
		
			echo  '<script type="text/javascript"> 
			alert("生成产品相关模板成功！");
			window.location.href="http://cms.wondershare.com/wondershare/faq_online_help.php?action=index&web_type='.$this->webType.'";
			</script>';
		}
	} 
}

$action    = $_REQUEST['action']; 
$faq_comment   = new Faq_New;
if (method_exists($faq_comment, $action)) {
	$faq_comment->$action();
} else {
	$faq_comment->index();
}

?>
